# -*- coding: UTF-8 -*- #
"""
-------真正的勇士， 敢于直面惨淡的Warning、 敢于正视淋漓的Error-----
@author : 贾鹏博
@file : 14、css 定位与原生定位.py
@time : 2022/5/27 22:42
"""

"""
原生定位
css selector

1、打开[雪球]应用首页
2、点击搜素框
3、向搜素框输入：alibaba
4、判断[阿里巴巴]可见
"""

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy


class Test_css():

    def setup(self):
        desired_caps = {}
        desired_caps['platformName'] = "Android"
        desired_caps['platformVersion'] = '6.0'
        desired_caps['deviceName'] = 'emulator-5554'
        desired_caps['appPackage'] = 'com.xueqiu.android'
        desired_caps['appActivity'] = 'com.xueqiu.android.common.MainActivity'
        desired_caps['noReset'] = 'true'
        # desired_caps['dontStopAppOnReset'] = 'true'
        desired_caps['skipDeviceInitialization'] = 'true'
        desired_caps['unicodeKeyBoard'] = 'true'
        desired_caps['resetKeyBoard'] = 'true'
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
        self.driver.implicitly_wait(10)

    def teardown(self):
        self.driver.quit()

    def test_css_search(self):
        element = self.driver.find_element(AppiumBy.CSS_SELECTOR, "#com\.xueqiu\.android\:id\/tv_search")
        search_enabled = element.is_enabled()
        print(f'搜素框的文本:{element.text},搜素框的坐标:{element.location},搜素框的size:{element.size}')
        if search_enabled == True:
            # 点击搜素框
            element.click()
            # 向搜素框输入：alibaba
            self.driver.find_element(AppiumBy.CSS_SELECTOR, "#com\.xueqiu\.android\:id\/search_input_text").send_keys(
                'alibaba')
            alibaba_element = self.driver.find_element(AppiumBy.CSS_SELECTOR, '*[text="阿里巴巴"]')
            displayed = alibaba_element.get_attribute('displayed')
            # 判断 [阿里巴巴] 可见
            print(displayed)
            assert displayed == "true"
